﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// WebHelper 的摘要说明
/// </summary>
public class WebHelper
{
	public WebHelper()
	{
		//
		// TODO: 在此处添加构造函数逻辑
		//
	}

    //弹出页面提示
    public static void Alert(string sMessage)
    {
        HttpContext.Current.Response.Write("<script>alert('" + sMessage + "');</script>");
    }
    //弹出页面提示并关闭此页面
    public static void AlertAndClose(string sMessage)
    {
        HttpContext.Current.Response.Write("<script>alert('" + sMessage + "');window.close();</script>");
        HttpContext.Current.Response.End();
    }
    //关闭页面
    public static void Close()
    {
        HttpContext.Current.Response.Write("<script>window.close();window.opener.close()</script>");
        HttpContext.Current.Response.End();
    }
    //弹出页面提示，并刷新页面
    public static void AlertAndRefresh(string sMessage)
    {
        HttpContext.Current.Response.Write("<script>alert('" + sMessage + "');location.href=location.href</script>");
    }
    public static void AlertAndCloseThenRefreshLastPage(string sMessage)
    {
        HttpContext.Current.Response.Write("<script>alert('" + sMessage + "');opener.location.reload();window.close();</script>");
        HttpContext.Current.Response.End();
    }
    //弹出页面提示，并跳转到新页面
    public static void AlertAndRedirect(string sMessage, string toURL)
    {
        string format = "<script language='JavaScript'>alert('{0}');window.location.replace('{1}')</script>";
        HttpContext.Current.Response.Write(string.Format(format, sMessage, toURL));
        HttpContext.Current.Response.End();
    }
    public static void PublishTickets(string username, string roles)
    {
        FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, username, DateTime.Now, DateTime.Now.AddMinutes(15.0), false, roles);
        string str = FormsAuthentication.Encrypt(ticket);
        HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, str);
        HttpContext.Current.Response.Cookies.Add(cookie);
    }
    public static bool IsUserLogout()
    {
        if (HttpContext.Current.User.Identity.Name == "" || HttpContext.Current.User.Identity.Name == null)
            return true;
        else return false;
    }

    //is the string is null or not
    public static bool IsNull(string value)
    {
        return (value == null || value == "") ? true : false;
    }
    //open a page
    public static void Open(string url)
    {
        HttpContext.Current.Response.Write("<script type='text/javascript'>window.open('" + url + "','','')</script>");
    }
    //output script
    public static void ResponseScript(string script)
    {
        HttpContext.Current.Response.Write("<script type='text/javascript'>" + script + "</script>");
    }
    //同比函数 （今年，去年）
    public static string TongBi(string thisvalue, string thatvalue)
    {
        float thisdata = Convert.ToSingle(thisvalue);
        float thatdata = Convert.ToSingle(thatvalue);
        if (thatdata == 0)
            return "";
        if (thisdata > thatdata)
        {
            return (Math.Abs(((thisdata - thatdata) / thatdata) * 100)).ToString();
        }
        else
        {
            return ((Math.Abs(((thisdata - thatdata) / thatdata) * 100))*(-1)).ToString();
        }
    }
    //输入的字符串是否都是数字
    public static bool IsNum(string value)
    {
        for (int i = 0; i < value.Length; i++)
        {
            if (value[i] < '0' || value[i] > '9')
                return false;
        }
        return true;
    }
  
}
